//package com.threefish.common.handler;
//
//import com.threefish.common.vo.ResultVO;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.http.HttpStatus;
//import org.springframework.web.bind.annotation.ControllerAdvice;
//import org.springframework.web.bind.annotation.ExceptionHandler;
//import org.springframework.web.bind.annotation.RestControllerAdvice;
//import org.springframework.web.servlet.ModelAndView;
//import org.springframework.web.servlet.NoHandlerFoundException;
//import org.springframework.web.servlet.view.json.MappingJackson2JsonView;
//
//import javax.servlet.ServletRequest;
//import javax.servlet.http.HttpServletRequest;
//
///**
// * @author 黄川 huchuc@vip.qq.com
// * @date: 2018/9/10
// * 统一错误控制
// */
//@ControllerAdvice
//@RestControllerAdvice
//@Slf4j
//public class GlobalExceptionHandler {
//
//    public static final String DEFAULT_ERROR_VIEW = "error/500.html";
//
//    public static final MappingJackson2JsonView JSON_VIEW = new MappingJackson2JsonView();
//
//    @ExceptionHandler
//    public ModelAndView defaultErrorHandler(HttpServletRequest req, Exception e) {
//        int status = 500;
//        if (e instanceof NoHandlerFoundException) {
//            status = 404;
//        }
//        ModelAndView mav;
//        if (isAjax(req)) {
//            /**
//             * 按照 ResultVO 返回结果
//             * @see ResultVO
//             */
//            mav = new ModelAndView(JSON_VIEW);
//            mav.addObject("ok", false);
//            mav.addObject("status", status);
//            mav.addObject("msg", e.getMessage());
//            mav.addObject("url", req.getRequestURL().toString());
//            mav.setStatus(HttpStatus.valueOf(status));
//        } else {
//            mav = new ModelAndView();
//            mav.addObject("exception", e);
//            mav.addObject("status", status);
//            mav.addObject("url", req.getRequestURL().toString());
//            mav.setViewName(DEFAULT_ERROR_VIEW);
//            mav.setStatus(HttpStatus.valueOf(status));
//        }
//        log.debug("全局错误捕捉：", e);
//        return mav;
//    }
//
//    public static boolean isAjax(ServletRequest req) {
//        String value = ((HttpServletRequest) req).getHeader("X-Requested-With");
//        return value != null && "XMLHttpRequest".equalsIgnoreCase(value.trim());
//    }
//}
